package com.yy.main.app.web;

import com.yy.kit.annotation.HasPers;
import com.yy.kit.collections.YYMapKits;
import com.yy.main.app.mapper.TUserMapper;
import com.yy.main.app.mapper.TUserRoleMapper;
import com.yy.main.app.service.IRoleService;
import com.yy.main.app.service.IUserRoleService;
import com.yy.main.app.service.IUserService;
import com.yy.main.app.web.base.CommonController;
import com.yy.pojo.base.TRole;
import com.yy.pojo.base.TUser;
import com.yy.pojo.base.TUserRoleExample;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户与角色的关联表 前端控制器
 * </p>
 *
 * @author 杨阳
 * @since 2017-08-22
 */
@Controller
@RequestMapping("/userrole")
@Api(value = "用户与角色的关联控制器", tags = {"用户与角色的关联控制器"})
public class UserRoleController extends CommonController {
    @Resource(name = "iUserService")
    private IUserService iUserService;
    @Resource(name = "iRoleService")
    private IRoleService iRoleService;
    @Resource(name = "iUserRoleService")
    private IUserRoleService iUserRoleService;
    @Autowired
    private TUserMapper tUserMapper;
    @Autowired
    private TUserRoleMapper tUserRoleMapper;

    /**
     * <p>
     * 跳转到用户分配角色的用户列表页面
     * </p>
     *
     * @param session 应用中的会话
     * @return {@link ModelAndView}
     * @author 杨阳
     * @since 2017年9月14日
     */
    @RequestMapping(value = "/userList", method = RequestMethod.GET)
    @HasPers(name = "userrole:manager")
    @ApiOperation(value = "跳转到用户分配角色的用户列表页面", httpMethod = "GET", notes = "跳转到用户分配角色的用户列表页面，必须拥有userrole:manager权限")
    public ModelAndView userList(HttpSession session) {
        return goToPageByModelAndView("system/userrole/user_assign_list", null, getCurrentUser(session).getId());
    }

    /**
     * <p>
     * 获得管理端用户的数据表格
     * </p>
     *
     * @param page    当前页
     * @param limit   每页显示多少条数据
     * @param status  用户状态
     * @param name    用户名
     * @param usernum 用户编号
     * @param sex     性别
     * @author 杨阳
     * @since 2017年9月14日
     */
    @RequestMapping(value = "/userTable", method = RequestMethod.GET)
    @ApiOperation(value = "获得管理端用户的数据表格", httpMethod = "GET", notes = "获得管理端用户的数据表格")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页数", dataType = "Integer", paramType = "path", required = true),
            @ApiImplicitParam(name = "limit", value = "每页显示记录数", dataType = "Integer", paramType = "path", required = true),
            @ApiImplicitParam(name = "status", value = "用户状态，1.正常 2.锁定", dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "name", value = "真实姓名", dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "usernum", value = "用户编号", dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "sex", value = "性别1.男 2.女", dataType = "Integer", paramType = "path")
    })
    @ResponseBody
    public Map<String, Object> userTable(Integer page, Integer limit, String status, String name, String usernum, String sex) {
        return super.pageToMap(iUserService.userAssignList(page, limit, status, usernum, name, sex));
    }

    /**
     * <p>
     * 显示用户及用户拥有的角色
     * </p>
     *
     * @return {@link ModelAndView}
     * @author 杨阳
     * @since 2017年9月15日
     */
    @RequestMapping(value = "/form/{id}", method = RequestMethod.GET)
    @HasPers(name = "userrole:assign")
    @ApiOperation(value = "显示用户及用户拥有的角色", httpMethod = "GET", notes = "用户必须拥有userrole:assign权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", dataType = "String", required = true, paramType = "url")
    })
    public ModelAndView form(@PathVariable("id") String id) {
        TUser user = tUserMapper.selectByPrimaryKey(id);
        List<TRole> roleList = iRoleService.userOwnRoleList(id);
        Map<String, Object> param = YYMapKits.createHashMap();
        param.put("user", user);
        param.put("roleList", roleList);
        return goToPageByModelAndView("system/userrole/user_assign", param);
    }

    /**
     * <p>
     * 为用户分配角色，这里暂时是一个用户只能分配一个角色
     * </p>
     *
     * @param role   角色id
     * @param userid 用户id
     * @author 杨阳
     * @since 2017年9月19日18:14:05
     */
    @ApiOperation(value = "为用户分配角色，这里暂时是一个用户只能分配一个角色", httpMethod = "POST", notes = "为用户分配角色，这里暂时是一个用户只能分配一个角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "role", value = "角色id", dataType = "String", required = true, paramType = "query"),
            @ApiImplicitParam(name = "userid", value = "用户id", dataType = "String", required = true, paramType = "query")
    })
    @RequestMapping(value = "/assign", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> assign(String role, String userid) {
        return iUserRoleService.assign(role, userid);
    }

    /**
     * <p>
     * 回收用户角色
     * </p>
     *
     * @param userid 用户id
     * @return {@link Map}<String, Object>
     * @author 杨阳
     * @since 2017年9月20日
     */
    @ApiOperation(value = "回收用户角色", httpMethod = "POST", notes = "回收用户角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userid", value = "用户id", dataType = "String", required = true, paramType = "path")
    })
    @RequestMapping(value = "/recovery/{userid}", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> recovery(@PathVariable("userid") String userid) {
        Map<String, Object> res = YYMapKits.createHashMap();

        TUserRoleExample example = new TUserRoleExample();
        example.createCriteria().andUser_idEqualTo(userid);

        Integer b = tUserRoleMapper.deleteByExample(example);
        if (b > 0) {
            res.put("success", Boolean.TRUE);
            res.put("msg", "回收成功!");
        } else {
            res.put("success", Boolean.FALSE);
            res.put("msg", "回收失败,请重试!");
        }
        return res;
    }
}
